package com.easy.dfc.web;

import com.alibaba.fastjson.JSONObject;
import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.core.web.BaseController;
import com.easy.dfc.Utils.JsonUtil;
import com.easy.dfc.Utils.SensitivewordEngine;
import com.easy.dfc.Utils.Util;
import com.easy.dfc.Utils.Error;
import com.easy.dfc.entity.*;
import com.easy.dfc.mapper.SensitiveWordsMapper;
import com.easy.dfc.service.AskOtherService;
import com.easy.dfc.service.UsersetService;
import com.easy.modules.sys.entity.Log;
import com.sun.media.jfxmedia.logging.Logger;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "${apiPath}/askother")
public class AskOtherController extends BaseController {

    @Resource
    private AskOtherService askOtherService;

    @Resource
    private UsersetService usersetService;

    /**
     * 发布问大家
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addQuestion", method = RequestMethod.POST)
    public Result addQuestion(@RequestBody ParaAskother para, HttpServletRequest request, Model model) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            String path = Util.UploadImages(request, "images");
            return askOtherService.addQuestion(para, path);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 获取问大家列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getQuestionAll", method = RequestMethod.POST)
    public Result getQuestionAll(@RequestBody ParaAskother para,HttpServletRequest request,Model model) {
        try {
            return askOtherService.getQuestionAll(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 获取问大家详情
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getQuestionDetail", method = RequestMethod.POST)
    public Result getQuestionDetail(@RequestBody ParaAskother para,HttpServletRequest request,Model model) {
        try {
            return askOtherService.getQuestionDetail(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 获取问大家列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getQuestionList", method = RequestMethod.POST)
    public Result getQuestionList(@RequestBody ParaAskother para,HttpServletRequest request,Model model) {
        try {
            return askOtherService.getQuestionList(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 获取我的问大家列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getMyQuestionList", method = RequestMethod.POST)
    public Result getMyQuestionList(@RequestBody ParaAskother para,HttpServletRequest request,Model model) {
        try {
            return askOtherService.getMyQuestionList(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 获取我参于问大家列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getMyAnswerQuestionList", method = RequestMethod.POST)
    public Result getMyAnswerQuestionList(@RequestBody ParaAskother para,HttpServletRequest request,Model model) {
        try {
            return askOtherService.getMyAnswerQuestionList(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }


    /**
     * 发布问大家的回答
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addAnswer", method = RequestMethod.POST)
    public Result addAnswer(@RequestBody ParaAskother para, HttpServletRequest request, Model model) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            //图片上传到阿里云
            String path = Util.UploadImages(request, "images");
            return askOtherService.addAnswer(para, path);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 获取回答列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getAnswerList", method = RequestMethod.POST)
    public Result getAnswerList(@RequestBody ParaAskother para,HttpServletRequest request,Model model) {
        try {
            return askOtherService.selectAnswerList(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 发布回答的回复
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addReply", method = RequestMethod.POST)
    public Result addReply(@RequestBody ParaAskother para, HttpServletRequest request, Model model) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            return askOtherService.addReply(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 获取评论回答列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getReplyList", method = RequestMethod.POST)
    public Result getReplyList(@RequestBody ParaAskother para,HttpServletRequest request,Model model) {
        try {
            return askOtherService.selectReplyList(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
}
